package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.core.domain.R;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.HisAdmittingDiagInfoBo;
import com.hospital.workstation.domain.vo.HisAdmittingDiagInfoVo;
import com.hospital.workstation.domain.HisAdmittingDiagInfo;
import com.hospital.workstation.mapper.HisAdmittingDiagInfoMapper;
import com.hospital.workstation.service.IHisAdmittingDiagInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 诊断信息Service业务层处理
 *
 * @author whb
 * @date 2023-03-07
 */
@RequiredArgsConstructor
@Service
public class HisAdmittingDiagInfoServiceImpl implements IHisAdmittingDiagInfoService {

    private final HisAdmittingDiagInfoMapper baseMapper;

    /**
     * 查询诊断信息
     */
    @Override
    public HisAdmittingDiagInfoVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询诊断信息列表
     */
    @Override
    public TableDataInfo<HisAdmittingDiagInfoVo> queryPageList(HisAdmittingDiagInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisAdmittingDiagInfo> lqw = buildQueryWrapper(bo);
        Page<HisAdmittingDiagInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询诊断信息列表
     */
    @Override
    public List<HisAdmittingDiagInfoVo> queryList(HisAdmittingDiagInfoBo bo) {
        LambdaQueryWrapper<HisAdmittingDiagInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisAdmittingDiagInfo> buildQueryWrapper(HisAdmittingDiagInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisAdmittingDiagInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, HisAdmittingDiagInfo::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagCode()), HisAdmittingDiagInfo::getDiagCode, bo.getDiagCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagType()), HisAdmittingDiagInfo::getDiagType, bo.getDiagType());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagDesc()), HisAdmittingDiagInfo::getDiagDesc, bo.getDiagDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagDocCode()), HisAdmittingDiagInfo::getDiagDocCode, bo.getDiagDocCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagDocDesc()), HisAdmittingDiagInfo::getDiagDocDesc, bo.getDiagDocDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagCategory()), HisAdmittingDiagInfo::getDiagCategory, bo.getDiagCategory());
        return lqw;
    }

    /**
     * 新增诊断信息
     */
    @Override
    public Boolean insertByBo(HisAdmittingDiagInfoBo bo) {
        HisAdmittingDiagInfo add = BeanUtil.toBean(bo, HisAdmittingDiagInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改诊断信息
     */
    @Override
    public Boolean updateByBo(HisAdmittingDiagInfoBo bo) {
        HisAdmittingDiagInfo update = BeanUtil.toBean(bo, HisAdmittingDiagInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisAdmittingDiagInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除诊断信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 查询诊断信息列表
     *
     * @param bo 诊断信息业务对象
     * @return
     */
    @Override
    public R<List<HisAdmittingDiagInfoVo>> queryDiagList(HisAdmittingDiagInfoBo bo) {
        LambdaQueryWrapper<HisAdmittingDiagInfo> lqw = buildQueryWrapper(bo);
        List<HisAdmittingDiagInfoVo> hisAdmittingDiagInfoVoList = baseMapper.selectVoList(lqw);
        return R.ok(hisAdmittingDiagInfoVoList);
    }
}
